package com.ruoyi.web.controller.module.event;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.event.EventMaintenanceCycle;
import com.ruoyi.system.service.module.event.IEventMaintenanceCycleService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 维保周期Controller
 *
 * @author ruoyi
 * @date 2023-12-26
 */
@Validated
@Api(value = "维保周期控制器", tags = {"维保周期管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/maintenance/cycle")
public class EventMaintenanceCycleController extends BaseController {

    private final IEventMaintenanceCycleService eventMaintenanceCycleService;

    /**
     * 查询维保周期列表
     */
    @ApiOperation("查询维保周期列表")
    @PreAuthorize("@ss.hasPermi('module:maintenanceCycle:list')")
    @GetMapping("/list")
    public TableDataInfo<EventMaintenanceCycle> list(@Validated EventMaintenanceCycle eventMaintenanceCycle) {
        QueryWrapper<EventMaintenanceCycle> qw = new QueryWrapper<>();

        TableDataInfo<EventMaintenanceCycle> td = PageUtils.buildDataInfo(eventMaintenanceCycleService.page(PageUtils.buildPage(),qw));
        List<EventMaintenanceCycle> list = td.getRows();
        return td;
    }

    /**
     * 导出维保周期列表
     */
    @ApiOperation("导出维保周期列表")
    @PreAuthorize("@ss.hasPermi('module:maintenanceCycle:export')")
    @Log(title = "维保周期", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<EventMaintenanceCycle> export(@Validated EventMaintenanceCycle eventMaintenanceCycle) {
        List<EventMaintenanceCycle> list = eventMaintenanceCycleService.list();
        ExcelUtil<EventMaintenanceCycle> util = new ExcelUtil<EventMaintenanceCycle>(EventMaintenanceCycle.class);
        return util.exportExcel(list, "维保周期");
    }

    /**
     * 获取维保周期详细信息
     */
    @ApiOperation("获取维保周期详细信息")
    @PreAuthorize("@ss.hasPermi('module:maintenanceCycle:query')")
    @GetMapping("/{id}")
    public AjaxResult<EventMaintenanceCycle> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Integer id) {
        EventMaintenanceCycle eventMaintenanceCycle = eventMaintenanceCycleService.getById(id);
        return AjaxResult.success(eventMaintenanceCycle);
    }

    /**
     * 新增维保周期
     */
    @ApiOperation("新增维保周期")
    @PreAuthorize("@ss.hasPermi('module:maintenanceCycle:add')")
    @Log(title = "维保周期", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody EventMaintenanceCycle eventMaintenanceCycle) {
        return toAjax(eventMaintenanceCycleService.save(eventMaintenanceCycle) ? 1 : 0);
    }

    /**
     * 修改维保周期
     */
    @ApiOperation("修改维保周期")
    @PreAuthorize("@ss.hasPermi('module:maintenanceCycle:edit')")
    @Log(title = "维保周期", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody EventMaintenanceCycle eventMaintenanceCycle) {
        return toAjax(eventMaintenanceCycleService.updateById(eventMaintenanceCycle) ? 1 : 0);
    }

    /**
     * 删除维保周期
     */
    @ApiOperation("删除维保周期")
    @PreAuthorize("@ss.hasPermi('module:maintenanceCycle:remove')")
    @Log(title = "维保周期" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Integer[] ids) {
        return toAjax(eventMaintenanceCycleService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
